<?php

namespace App\Http\Controllers;

use App\Helpers\Bing;
use App\Models\Picture;
use Illuminate\Http\Request;

class ApiController extends Controller
{
    public function today(): array
    {
        $date = date('Y-m-d');
        $pic = Picture::where('datetime', $date)->first();
        if (empty($pic)) {
            return rsp($code = 40004);
        }
        return rsp($pic);
    }

    public function all(Request $request)
    {
        $order = $request->get('order', 'asc');
        $limit = $request->get('limit', 10);
        $items = Picture::orderBy('datetime', $order)->paginate($limit);
        return rsp($items);
    }

    public function random()
    {
        $item = Picture::inRandomOrder()->first();
        return rsp($item);
    }

    public function rank(Request $request)
    {
        $type = $request->get('type', 1);  // 1: 浏览量，2：赞，3：下载量
        switch ($type) {
            case 2: {
                $key = 'like';
                break;
            }
            case 3: {
                $key = 'down';
                break;
            }
            default: {
                $key = 'view';
                break;
            }
        }
        $limit = $request->get('limit', 10);
        $items = Picture::orderBy($key, 'desc')->paginate($limit);
        return rsp($items);
    }
}
